<template>
  <div>
    <u-form :label-style="{'font-weight':'bold'}" :model="orderDeliveryForm" class="form" label-width="150" ref="uForm">
      <u-form-item label="物流公司" @click="show=true" prop="logisticsName">
        <u-input type="select" @click="show=true" v-model="orderDeliveryForm.logisticsName" />
      </u-form-item>
      <u-form-item label="物流单号" prop="logisticsNo">
        <u-input v-model="orderDeliveryForm.logisticsNo" />
      </u-form-item>

      <div class="submit" @click="update">
        发货
      </div>
    </u-form>
    <u-select confirm-color="#333" @confirm="confirmLogistics" v-model="show" :list="checkedLogistics"></u-select>
  </div>
</template>

<script>
import { getLogisticsChecked, orderDelivery } from "@/api/order";

export default {
  data() {
    return {
      show: false, //是否显示快递
      rules: {
        logisticsNo: [
          { required: true, message: "发货单号不能为空", trigger: "change" },
        ],
        logisticsId: [
          { required: true, message: "请选择物流公司", trigger: "blur" },
        ],
        logisticsName: [
          { required: true, message: "请选择物流公司", trigger: "blur" },
        ],
      },
      checkedLogistics: [], //选中的物流公司集合
      //订单发货

      orderDeliveryForm: {
        logisticsName: "",
        logisticsNo: "", //发货单号
        logisticsId: "", //物流公司
      },
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad(options) {
	  if(options.data){
		  this.routerVal = JSON.parse(decodeURIComponent(options.data));
		  this.init();
	  }
  },

  methods: {
    //   初始化
    async init() {
      let res = await getLogisticsChecked();
      if (res.data.success) {
        this.checkedLogistics = res.data.result.map((item) => {
          return { value: item.id, label: item.name };
        });
      }
    },
    // 修改/更新数据
    update() {
      this.$refs.uForm.validate(async (valid) => {
        if (valid) {
          let orderDeliveryForm = JSON.parse(
            JSON.stringify(this.orderDeliveryForm)
          );
          delete orderDeliveryForm.logisticsName;
          uni.showLoading({
            title: "加载中",
            mask: "true",
          });

          let res = await orderDelivery(this.routerVal.sn, orderDeliveryForm);
          if (res.data.success) {
            uni.showToast({
              title: "发货成功",
              duration: 2000,
              icon: "none",
            });

            setTimeout(() => {
              uni.navigateBack();
            }, 500);
          }
          uni.hideLoading();
        }
      });
    },

    // 确定地址
    confirmLogistics(val) {
      this.orderDeliveryForm.logisticsId = val[0].value;
      this.orderDeliveryForm.logisticsName = val[0].label;
    },
  },
};
</script>

<style scoped lang="scss">
@import "./style.scss";
.form {
  padding: 32rpx;
  background: #fff;
}
</style>